﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Globalization;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmDonVi : DevExpress.XtraEditors.XtraForm
    {
        #region khai bao bien

        public string maDonVi="";
        DataTable dt;
        DonVi_BLL dv_bll = new DonVi_BLL();
        TextMessageBox box=new TextMessageBox();
        private bool isOk = false;
             
        #endregion

        #region LoadForm

        public frmDonVi()
        {
            InitializeComponent();
        }
        private void frmDonVi_Load(object sender, EventArgs e)
        {
            Load_Data();
        }
        #endregion

        #region Cac chuc nang

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            UpdateData();
        }
       
        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            UpdateData();
           
        }
        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }
        #endregion

        #region ham viet them
        private void UpdateData()
        {
            if (checkValid())
            {
                DonVi_Public dv = new DonVi_Public();
                if (maDonVi == "")//them moi
                {
                    if (checkTenInsert())
                    {
                        dv.MaDonVi = box.AutoID("DV", 2, dv_bll.LayDuLieu());
                        dv.TenDonVi = box.toUpper(txtTenDonVi.Text);
                        dv.GhiChu = txtGhiChu.Text;

                        dv_bll.Insert(dv, dv, dv);
                        CloseForm();
                    }
                }
                else//update
                {
                    if (checkTenUpdate())
                    {
                        dv.MaDonVi = txtMaDonVi.Text.Trim();
                        dv.TenDonVi = box.toUpper(txtTenDonVi.Text);
                        dv.GhiChu = txtGhiChu.Text;

                        dv_bll.Update(dv, dv, dv);
                        CloseForm();
                    }
                }

            }
        }
        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void Load_Data()
        {
            if (maDonVi != "")
            {
                
                DataTable tbl = dv_bll.TaoBang(box.sqlCheckExist("DM_DonVi","MaDonVi",maDonVi));
                DataRow dr = tbl.Rows[0];
                txtMaDonVi.Text = maDonVi;
                txtTenDonVi.Text = dr["TenDonVi"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
                
            }
            
        }

        private bool checkValid()
        {
            if (txtTenDonVi.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên đơn vị.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = dv_bll.TaoBang(box.sqlCheckExist("DM_DonVi", "TenDonVi", txtTenDonVi.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenDonVi.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTenDonVi.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenUpdate()
        {
            dt = dv_bll.TaoBang(box.sqlCheckExist("DM_DonVi", "TenDonVi", txtTenDonVi.Text.Trim()));
            if (dt.Rows.Count +1> 1)
            {
                XtraMessageBox.Show(box.Exist(txtTenDonVi.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTenDonVi.Focus();
                return false;
            }
            return true;
        }
        private void SetInterface()
        {
            txtMaDonVi.Text = "";
            txtTenDonVi.Text = "";
            txtGhiChu.Text = "";
            maDonVi = "";
            txtMaDonVi.Text="Mã tự sinh";
        }
        #endregion

        #region cac su kien

        private void frmDonVi_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                this.DialogResult = DialogResult.OK;
        }
        #endregion


    }
}